package com.ruoyi.dm.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SceneUtil;
import com.ruoyi.dm.domain.TrackTleVO;
import com.ruoyi.dm.domain.po.CollectTleRecordPO;
import com.ruoyi.dm.domain.po.TrackTleParam;
import com.ruoyi.dm.mapper.CollectTleRecordMapper;
import com.ruoyi.dm.service.ICollectTleRecordService;
import com.ruoyi.utils.TranferUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CollectTleRecordServiceImpl extends ServiceImpl<CollectTleRecordMapper, CollectTleRecordPO> implements ICollectTleRecordService {

    @Autowired
    private CollectTleRecordMapper collectTleRecordMapper;

    @Override
    public void repeatRemove() {
        int i = collectTleRecordMapper.repeatRemove();
        log.info("除重:{}",i);
    }

    @Override
    public IPage queryByParam(  TrackTleParam param) {
        LambdaQueryWrapper<CollectTleRecordPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(param.getCode()),CollectTleRecordPO::getTleCode,param.getCode());
        String scene = param.getScene();
        if (StringUtils.hasLength(scene)) {
            List<DateTime> deal = SceneUtil.deal(scene);
            DateTime start = deal.get(0);
            DateTime end = deal.get(1);
            queryWrapper.between(CollectTleRecordPO::getDataTime,start,end);
        }else {
            queryWrapper.between(CollectTleRecordPO::getDataTime,param.getStartTime(),param.getEndTime());
        }

        queryWrapper.orderByDesc(CollectTleRecordPO::getDataTime);
        IPage page = this.page(param.page(), queryWrapper);
        List records = page.getRecords();
        if (records != null) {
            List<TrackTleVO> resList = new ArrayList<>();
            for (Object record : records) {
                CollectTleRecordPO po = (CollectTleRecordPO)record;
                TrackTleVO vo = TranferUtils.tranferTlePO(po);
                resList.add(vo);
            }
            page.setRecords(resList);
        }
        return page;
    }
}
